package com.contest.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.entity.GameNumbersRule;
import com.contest.service.GameNumbersRuleService;
import com.contest.util.IdWorker;
import com.contest.util.PageUtils;
import com.contest.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.contest.entity.GameNumbersResult;
import com.contest.service.GameNumbersResultService;


/**
 * 
 *
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-05-17 14:57:27
 */

@Controller
@RequestMapping("/gameNumbersResult")
public class GameNumbersResultController {

    @Autowired
    GameNumbersResultService gameNumbersResultService;
    @Autowired
    GameNumbersRuleService gameNumbersRuleService;
    @Autowired
    IdWorker idWorker;

    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String,Object> param) {
        int page = Integer.parseInt(param.get("page").toString());
        int rows = Integer.parseInt(param.get("rows").toString());
        Page<GameNumbersResult> mPage = new Page<GameNumbersResult>(page , rows);
        param.remove("page");
        param.remove("rows");
        Wrapper<GameNumbersResult> w = new EntityWrapper<>();
        Set<String> keys = param.keySet();
        for (String key : keys) {
            Object value = param.get(key);
            if (value != null && !"".equals(value)) {
                w.like(key, value.toString());
            }
        }
        w.orderBy("fd_date" , false);
        w.orderBy("fd_preiod_index" , true);
        mPage = gameNumbersResultService.selectPage(mPage , w);
        return new PageUtils(mPage.getRecords(),mPage.getTotal());
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(GameNumbersResult gameNumbersResult) {
        gameNumbersResult.setFdId(idWorker.nextId() + "");

        EntityWrapper<GameNumbersRule> ewRule = new EntityWrapper<>();
        ewRule.eq("fd_date" , gameNumbersResult.getFdDate());
        GameNumbersRule rule = (GameNumbersRule) gameNumbersRuleService.selectOne(ewRule);
        if(rule == null)
            return R.error("期号日期不存在");
        else
            gameNumbersResult.setFdRuleId(rule.getFdId());

        EntityWrapper<GameNumbersResult> ewResult = new EntityWrapper<>();
        ewResult.eq("fd_date" , gameNumbersResult.getFdDate());
        ewResult.eq("fd_preiod_index" , gameNumbersResult.getFdPreiodIndex());
        GameNumbersResult result = (GameNumbersResult) gameNumbersResultService.selectOne(ewResult);
        if(result != null)
            return R.error("期号的期数已存在");

        if (gameNumbersResultService.insert(gameNumbersResult)) {
            return R.ok("操作成功!");
        } else {
            return R.error("操作失败");
        }
    }

    @GetMapping("/edit/{fdId}")
    public String edit(Model model, @PathVariable("fdId") String fdId) {
        GameNumbersResult gameNumbersResult = gameNumbersResultService.selectById(fdId);
        model.addAttribute("gameNumbersResult", gameNumbersResult);
        return "gameNumbersResult/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") String[] ids) {
        List<String> fdIds = new ArrayList<String>();
        for (String id : ids) {
            fdIds.add(id);
        }
        if (gameNumbersResultService.deleteBatchIds(fdIds)) {
            return R.ok("删除成功");
        } else {
            return R.error(-1, "删除失败!");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(GameNumbersResult gameNumbersResult) {
        if (gameNumbersResultService.updateById(gameNumbersResult)) {
            return R.ok("更新成功!");
        } else {
            return R.error(-1, "更新失败!");
        }
    }



}
